package com.caipiao.util;

/**
 * @author GaoFeng
 * @time 2014年1月13日
 */

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class IdMaker {
    private static final int MAX_VALUE = 999998;
    private static final String FORMAT14 = "yyyyMMddHHmmss";
    private static final Format DF14 = new SimpleDateFormat(FORMAT14);
    private static final String FORMAT8 = "yyyyMMdd";    //日
    private static final Format DF8 = new SimpleDateFormat(FORMAT8);
    private static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String letterChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String numberChar = "0123456789";
    static long sn = 1;

    public static String Get_Random_Id(int length) {
        return Get_Random_Id(length, 0);
    }

    //类型:0大小写数字，1大小写，2数字
    public static String Get_Random_Id(int length, int type) {
        String src = "";
        if (type == 0) {
            src = allChar;
        } else if (type == 1) {
            src = letterChar;
        } else if (type == 2) {
            src = numberChar;
        } else {
            src = allChar;
        }

        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(src.charAt(random.nextInt(src.length())));
        }
        return sb.toString();
    }

    // 新增：生成指定长度的纯数字编号
    public static String Get_Number_Id(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(numberChar.charAt(random.nextInt(numberChar.length())));
        }
        return sb.toString();
    }

    // 新增：生成带前缀的随机编号
    public static String Get_Prefix_Id(String prefix, int length) {
        return prefix + Get_Random_Id(length, 2);
    }

    // 新增：生成时间戳+随机数的组合ID
    public static String Get_Timestamp_Id(int randomLength) {
        return System.currentTimeMillis() + Get_Random_Id(randomLength, 2);
    }

    // 新增：生成较短的UUID（8位）
    public static String Get_Short_UUID() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 8);
    }

    public static String Get_Recharge_OrderId() {
        return Get_Order_Id();
    }

    public static String Get_Trade_No() {
        return Get_Order_Id();
    }

    public static String Get_Order_Id() {
        return "TRN" + GetSN();
    }

    public static String Get_Ticket_Id() {
        return "TCK" + GetSN();
    }

    public static String Get_LPS_MSGID() {
        return "LPS" + GetSN();
    }

    public static String Get_SMS_MSGID() {
        return "SMS" + GetSN();
    }

    public static String Get_SN() {
        return GetSN();
    }

    public static String Get_DB_Id() {
        return "DB" + GetSN();
    }

    public static String Get_MSG_Id() {
        return "MSG" + GetSN();
    }

    public static String TimeFormat() {
        Date date = new Date();
        return DF14.format(date);
    }

    public static String DateFormat() {
        Date date = new Date();
        return DF8.format(date);
    }

    public static String LastDateFormat() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);

        Date date = cal.getTime();

        return DF8.format(date);
    }

    public static synchronized String GetSN() {
        sn = (sn + 1) % MAX_VALUE;
        return TimeFormat() + String.format("%06d", sn);
    }

    public static String GetUUID32() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(IdMaker.GetUUID32());
        }

    }
}